module.exports = (app, router) => {
    const User = require('../../models/adminUser')
    const CryptoJS = require("crypto-js");
    const jwt = require('jsonwebtoken')
    const { requestResult } = require('../../plugins/index')
    //创建账户
    router.post('/createUser', async (req, res) => {
        const isExist = await User.find().countDocuments()
        if(isExist){
            res.json({
                status: 2,
                message: '管理员只能存在一个,无法创建多个'
            })
        }else{
            //密码加密 AES每次加密后的都不一样再不解密的情况下无法判断用户密码输入是否正确
            //let pwd = CryptoJS.AES.encrypt(req.body.userPwd, 'blog').toString();
            let pwd = CryptoJS.MD5(req.body.userPwd).toString().toUpperCase()
            let info = {
                userName: req.body.userName,
                userPwd: pwd
            }
            await User.create(info, (err, item) => {
                if(item.length != 0){
                    res.json({
                        status: 1,
                        message: '账号创建成功!!'
                    })
                }else{
                    res.json({
                        status: 2,
                        message: '账号创建失败,请稍后重试!!'
                    })
                }
            })
        }
    })
    //登录
    router.post('/login', async (req, res) => {
        //let pwd = CryptoJS.AES.encrypt(req.body.userPwd, 'blog').toString();
        console.log(req.body);
        let pwd = CryptoJS.MD5(req.body.userPwd).toString().toUpperCase()
        console.log(pwd);
        let info = {
            userName: req.body.userName,
            userPwd: pwd
        }
        //解密
        // const bytes  = CryptoJS.AES.decrypt(pwd, 'blog');
        // const decryptedData = bytes.toString(CryptoJS.enc.Utf8);
        User.find(info, (err, item) => {
            if(item && item.length){
                const token = jwt.sign(info, 'hello' ,{
                     expiresIn: 60 * 60 *24
                })
                res.send({
                    status: 1,
                    message: '登录成功',
                    token: `Bearer ${token}`
                })
            }else{
                res.send({
                    status: 2,
                    message: '账号或密码错误'
                })
            }
        })
    })
    //修改密码
    router.post('/updatePwd', async (req, res) => {
        console.log(req.body);
        let { oldPwd, newPwd, newPwds } = req.body
        if(newPwd != newPwds){
            res.send({
                status: 2,
                message: '两次输入的新密码不一致'
            })
        }else{
            oldPwd = CryptoJS.MD5(oldPwd).toString().toUpperCase()
            newPwd = CryptoJS.MD5(newPwd).toString().toUpperCase()
            console.log(oldPwd);
            User.findOne({ userPwd: oldPwd }, (err, item) => {
                console.log(item);
                if(item){
                    User.findOneAndUpdate({ userPwd: oldPwd}, { $set: { userPwd: newPwd } }, (err, ite) => {
                        if(ite){
                            res.send({
                                status: 1,
                                message: '密码修改成功'
                            })
                        }else{
                            res.send({
                                status: 2,
                                message: '密码修改失败'
                            })
                        }
                    })
                }else{
                    res.send({
                        status: 2,
                        message: '旧密码输入错误'
                    })
                }
            })
        }
    })

    //修改管理员头像
    router.post('/uploadAvater', async (req, res) => {
        await User.findOneAndUpdate({ 'userName': req.body.userName}, { 'avater': req.body.avater}, (err, item) => {
            res.send(requestResult(item))
        })
    })

    router.get('/sendCode', async (req, res) => {
        
    })
    app.use('/admin/api', router)
}